package com.lcb.manager.web;

import com.alibaba.fastjson.JSON;
import com.lcb.manager.configuration.Config;
import com.lcb.manager.entity.*;
import com.lcb.manager.repository.*;
import com.lcb.manager.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("android")
public class AndroidController {

    @Autowired
    private CoalRepository coalRepository;

    @Autowired
    private CoalOutputRepository coalOutputRepository;

    @Autowired
    private ChemicalRepository chemicalRepository;

    @Autowired
    private ChemicalProductRepository chemicalProductRepository;

    @Autowired
    private ChemicalProductOutputRepository chemicalProductOutputRepository;

    @Autowired
    private ElectricityRepository electricityRepository;

    @Autowired
    private ElectricityOutputRepository electricityOutputRepository;

    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private CompanyRepository companyRepository;


    /*****************************************************************************************************************/
    /* android 接口 */
    @ResponseBody
    @RequestMapping(value = "/coal_factory_name")
    public String getCoalFactoryName() {
        List<Coal> coalList = coalRepository.findAll();
        CoalVo vo = new CoalVo();
        vo.setName1(coalList.get(0).getName());
        vo.setName2(coalList.get(1).getName());
        vo.setName3(coalList.get(2).getName());
        vo.setName4(coalList.get(3).getName());
        vo.setName5(coalList.get(4).getName());
        return JSON.toJSONString(vo);
    }

    @ResponseBody
    @RequestMapping(value = "/coal_month_output")
    public String getCoalMonthOutput(@RequestParam int year, @RequestParam int month) {
        List<CoalOutput> coalOutputList = coalOutputRepository.findByMonth(year, month);
        List<MonthOutputVo> voList = new ArrayList<>();
        for (int i = 1; i <= Config.monthCountArray[month]; i++) {
            int finalI = i;
            List<Integer> outputList = new ArrayList<>();
            CoalOutput coalOutput = coalOutputList.stream().filter(x -> {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(x.getDate());
                int date = calendar.get(Calendar.DATE);
                return date == finalI;
            }).findFirst().orElse(null);
            if (coalOutput == null) {
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
            } else {
                outputList.add(coalOutput.getOutput1());
                outputList.add(coalOutput.getOutput2());
                outputList.add(coalOutput.getOutput3());
                outputList.add(coalOutput.getOutput4());
                outputList.add(coalOutput.getOutput5());
            }
            MonthOutputVo vo = new MonthOutputVo(i, 5, outputList);
            voList.add(vo);
        }
        return JSON.toJSONString(voList);
    }

    @ResponseBody
    @RequestMapping(value = "/coal_year_output")
    public String getCoalYearOutput(@RequestParam int year) {
        List<CoalOutput> coalOutputList = coalOutputRepository.findByYear(year);
        Map<Integer, List<CoalOutput>> groupByMonth = coalOutputList.stream().collect(Collectors.groupingBy(x -> {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(x.getDate());
            return calendar.get(Calendar.MONTH);
        }));
        List<YearOutputVo> voList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            List<CoalOutput> monthList = groupByMonth.get(i);
            List<Integer> outputList = new ArrayList<>();
            if (monthList == null || monthList.isEmpty()) {
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
            } else {
                int output1 = 0, output2 = 0, output3 = 0, output4 = 0, output5 = 0;
                for (CoalOutput coalOutput : monthList) {
                    output1 += coalOutput.getOutput1();
                    output2 += coalOutput.getOutput2();
                    output3 += coalOutput.getOutput3();
                    output4 += coalOutput.getOutput4();
                    output5 += coalOutput.getOutput5();
                }
                outputList.add(output1);
                outputList.add(output2);
                outputList.add(output3);
                outputList.add(output4);
                outputList.add(output5);
            }
            YearOutputVo vo = new YearOutputVo(i, 5, outputList);
            voList.add(vo);
        }
        return JSON.toJSONString(voList);
    }
    /*****************************************************************************************************************/


    /*****************************************************************************************************************/
    /* chemical 接口 */
    @ResponseBody
    @RequestMapping(value = "/chemical_factory_name")
    public String getChemicalFactoryName() {
        List<Chemical> chemicalList = chemicalRepository.findAll();
        ChemicalVo vo = new ChemicalVo();
        vo.setName1(chemicalList.get(0).getName());
        vo.setName2(chemicalList.get(1).getName());
        return JSON.toJSONString(vo);
    }

    @ResponseBody
    @RequestMapping(value = "/chemical_product_name")
    public String getChemicalProductName() {
        List<ChemicalProduct> chemicalProductList = chemicalProductRepository.findAll();
        ChemicalProductVo vo = new ChemicalProductVo();
        vo.setId(chemicalProductList.get(0).getId());
        vo.setName1(chemicalProductList.get(0).getName1());
        vo.setName2(chemicalProductList.get(0).getName2());
        vo.setName3(chemicalProductList.get(0).getName3());
        vo.setName4(chemicalProductList.get(0).getName4());
        vo.setName5(chemicalProductList.get(0).getName5());
        vo.setName6(chemicalProductList.get(0).getName6());
        vo.setName7(chemicalProductList.get(0).getName7());
        vo.setName8(chemicalProductList.get(0).getName8());
        return JSON.toJSONString(vo);
    }

    @ResponseBody
    @RequestMapping(value = "/chemical_month_output")
    public String getChemicalMonthOutput(@RequestParam int year, @RequestParam int month) {
        List<ChemicalProductOutput> chemicalProductOutputList = chemicalProductOutputRepository.findByMonth(year, month);
        List<MonthOutputVo> voList = new ArrayList<>();
        for (int i = 1; i <= Config.monthCountArray[month]; i++) {
            int finalI = i;
            List<Integer> outputList = new ArrayList<>();
            ChemicalProductOutput chemicalProductOutput = chemicalProductOutputList.stream().filter(x -> {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(x.getDate());
                int date = calendar.get(Calendar.DATE);
                return date == finalI;
            }).findFirst().orElse(null);
            if (chemicalProductOutput == null) {
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
            } else {
                outputList.add(chemicalProductOutput.getOutput1());
                outputList.add(chemicalProductOutput.getOutput2());
                outputList.add(chemicalProductOutput.getOutput3());
                outputList.add(chemicalProductOutput.getOutput4());
                outputList.add(chemicalProductOutput.getOutput5());
                outputList.add(chemicalProductOutput.getOutput6());
                outputList.add(chemicalProductOutput.getOutput7());
                outputList.add(chemicalProductOutput.getOutput8());
            }
            MonthOutputVo vo = new MonthOutputVo(i, 8, outputList);
            voList.add(vo);
        }
        return JSON.toJSONString(voList);
    }

    @ResponseBody
    @RequestMapping(value = "/chemical_year_output")
    public String getChemicalYearOutput(@RequestParam int year) {
        List<ChemicalProductOutput> coalOutputList = chemicalProductOutputRepository.findByYear(year);
        Map<Integer, List<ChemicalProductOutput>> groupByMonth = coalOutputList.stream().collect(Collectors.groupingBy(x -> {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(x.getDate());
            return calendar.get(Calendar.MONTH);
        }));
        List<YearOutputVo> voList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            List<ChemicalProductOutput> monthList = groupByMonth.get(i);
            List<Integer> outputList = new ArrayList<>();
            if (monthList == null || monthList.isEmpty()) {
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
            } else {
                int output1 = 0, output2 = 0, output3 = 0, output4 = 0, output5 = 0, output6 = 0, output7 = 0, output8 = 0;
                for (ChemicalProductOutput chemicalProductOutput : monthList) {
                    output1 += chemicalProductOutput.getOutput1();
                    output2 += chemicalProductOutput.getOutput2();
                    output3 += chemicalProductOutput.getOutput3();
                    output4 += chemicalProductOutput.getOutput4();
                    output5 += chemicalProductOutput.getOutput5();
                    output6 += chemicalProductOutput.getOutput6();
                    output7 += chemicalProductOutput.getOutput7();
                    output8 += chemicalProductOutput.getOutput8();
                }
                outputList.add(output1);
                outputList.add(output2);
                outputList.add(output3);
                outputList.add(output4);
                outputList.add(output5);
                outputList.add(output6);
                outputList.add(output7);
                outputList.add(output8);
            }
            YearOutputVo vo = new YearOutputVo(i, 8, outputList);
            voList.add(vo);
        }
        return JSON.toJSONString(voList);
    }

    /*****************************************************************************************************************/
    /* electricity 接口 */
    @ResponseBody
    @RequestMapping(value = "/electricity_factory_name")
    public String getElectricityFactoryName() {
        List<Electricity> electricityList = electricityRepository.findAll();
        ElectricityVo vo = new ElectricityVo();
        vo.setName1(electricityList.get(0).getName());
        vo.setName2(electricityList.get(1).getName());
        vo.setName3(electricityList.get(2).getName());
        vo.setName4(electricityList.get(3).getName());
        vo.setName5(electricityList.get(4).getName());
        vo.setName6(electricityList.get(5).getName());
        vo.setName7(electricityList.get(6).getName());
        return JSON.toJSONString(vo);
    }

    @ResponseBody
    @RequestMapping(value = "/electricity_month_output")
    public String getElectricityMonthOutput(@RequestParam int year, @RequestParam int month) {
        List<ElectricityOutput> electricityOutputList = electricityOutputRepository.findByMonth(year, month);
        List<MonthOutputVo> voList = new ArrayList<>();
        for (int i = 1; i <= Config.monthCountArray[month]; i++) {
            int finalI = i;
            List<Integer> outputList = new ArrayList<>();
            ElectricityOutput electricityOutput = electricityOutputList.stream().filter(x -> {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(x.getDate());
                int date = calendar.get(Calendar.DATE);
                return date == finalI;
            }).findFirst().orElse(null);
            if (electricityOutput == null) {
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
            } else {
                outputList.add(electricityOutput.getOutput1());
                outputList.add(electricityOutput.getOutput2());
                outputList.add(electricityOutput.getOutput3());
                outputList.add(electricityOutput.getOutput4());
                outputList.add(electricityOutput.getOutput5());
                outputList.add(electricityOutput.getOutput6());
                outputList.add(electricityOutput.getOutput7());
            }
            MonthOutputVo vo = new MonthOutputVo(i, 7, outputList);
            voList.add(vo);
        }
        return JSON.toJSONString(voList);
    }

    @ResponseBody
    @RequestMapping(value = "/electricity_year_output")
    public String getElectricityYearOutput(@RequestParam int year) {
        List<ElectricityOutput> electricityOutputList = electricityOutputRepository.findByYear(year);
        Map<Integer, List<ElectricityOutput>> groupByMonth = electricityOutputList.stream().collect(Collectors.groupingBy(x -> {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(x.getDate());
            return calendar.get(Calendar.MONTH);
        }));
        List<YearOutputVo> voList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            List<ElectricityOutput> monthList = groupByMonth.get(i);
            List<Integer> outputList = new ArrayList<>();
            if (monthList == null || monthList.isEmpty()) {
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
                outputList.add(0);
            } else {
                int output1 = 0, output2 = 0, output3 = 0, output4 = 0, output5 = 0, output6 = 0, output7 = 0;
                for (ElectricityOutput electricityOutput : monthList) {
                    output1 += electricityOutput.getOutput1();
                    output2 += electricityOutput.getOutput2();
                    output3 += electricityOutput.getOutput3();
                    output4 += electricityOutput.getOutput4();
                    output5 += electricityOutput.getOutput5();
                    output6 += electricityOutput.getOutput6();
                    output7 += electricityOutput.getOutput7();
                }
                outputList.add(output1);
                outputList.add(output2);
                outputList.add(output3);
                outputList.add(output4);
                outputList.add(output5);
                outputList.add(output6);
                outputList.add(output7);
            }
            YearOutputVo vo = new YearOutputVo(i, 7, outputList);
            voList.add(vo);
        }
        return JSON.toJSONString(voList);
    }

    /*****************************************************************************************************************/
    //在建项目
    @ResponseBody
    @RequestMapping(value = "/projects", method = RequestMethod.GET)
    public String getProjectInfo(@RequestParam int index) {
        Project project = null;
        String info;
        List<Project> projectList = projectRepository.findAll();
        if (index < 0) {
            index = 0;
            info = "已经是第一个了";
            project = projectList.get(0);
        } else if (index > projectList.size() - 1) {
            index = projectList.size() - 1;
            info = "已经是最后一个了";
            project = projectList.get(projectList.size() - 1);
        } else {
            info = "null";
            project = projectList.get(index);
        }
        ProjectVo projectVo = ProjectVo.fromEntity(project);
        projectVo.setIndex(index);
        projectVo.setInfo(info);
        return JSON.toJSONString(projectVo);
    }

    //企业简介
    @ResponseBody
    @RequestMapping(value = "/companies", method = RequestMethod.GET)
    public String getCompanyInfo(@RequestParam int num) {
        List<Company> companyList = companyRepository.findByNum(num);
        List<CompanyVo> companyVoList = companyList.stream().map(CompanyVo::fromEntity).collect(Collectors.toList());

        return JSON.toJSONString(companyVoList);
    }
}
